package com.bw.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.bw.bean.Classes;
import com.bw.bean.NewView;
import com.bw.bean.Students;
import com.bw.bean.Teachers;
import com.bw.bean.ViewVo;
import com.bw.service.ClassesService;
import com.bw.service.StudentsService;
import com.bw.service.TeachersService;

@Controller
@RequestMapping("chart")
public class ChartController {
	@Autowired
	private ClassesService classesServiceImpl;
	@Autowired
	private TeachersService teachersServiceImpl;
	@Autowired
	private StudentsService studentsServiceImpl;
	@RequestMapping("cList")
	public String cList(Model model){
		List<Classes> cList = classesServiceImpl.list();
		model.addAttribute("cList", cList);
		return "cList";
	}
	@RequestMapping("sList")
	public String sList(Model model){
		List<Students> sList = studentsServiceImpl.list();
		model.addAttribute("sList", sList);
		return "sList";
	}
	@RequestMapping("tList")
	public String tList(Model model){
		List<Teachers> tList = teachersServiceImpl.list();
		model.addAttribute("tList", tList);
		return "tList";
	}
	@RequestMapping("/myChart")
	@ResponseBody
	public String myChart(@RequestParam(defaultValue="1",value="i")int i, Model model, int cid, String start, String end) throws ParseException{
		Map<String, Object> map = new HashMap<String, Object>();
		String ziduan = null;
		if(i==1){
			ziduan = "面试量";
		}else{
			ziduan = "诊断量";
		}
		if(start == null){
			}
			if(end == null){
				end = new Date().toLocaleString();
			}
		List<ViewVo> list = classesServiceImpl.myChart(i, cid, start, end);
		List<NewView> newList = new ArrayList<NewView>(list.size());
		NewView newView = null;
		for (ViewVo viewVo : list) {
			newView = new NewView();
			newView.setCount(viewVo.getCount());
			String newDate  = viewVo.getDate().toLocaleString();
			newDate = newDate.substring(0,9);
			newView.setDate(newDate);
			newList.add(newView);
		}
		map.put("ziduan", ziduan);
		map.put("list", newList);
		return JSON.toJSONString(map);
	}
	@RequestMapping("/")
	public String chart(){
		return "viewChart";
	}
	
}
